package com.chenyijie.pojo;

import com.chenyijie.ServiceAbility;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.*;

/**
 * 骨架 远程代理对象类
 */
public class MyRemoteObject extends RemoteObject implements IRemoteBroker {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00001, MyRemoteObject.class.getSimpleName());
    public static final int DATACODE = 0x001;
    //请求的token
    public static final String DATAINTERFACETOKEN = "null";
    //响应的token
    public static final String REPLYINTERFACETOKEN = "404";

    public MyRemoteObject(){
        super("description.");
    }

    @Override
    public IRemoteObject asObject() {
        return this;
    }

    /**
     * 这个方法就是远程调用时的方法（响应 答复）
     * @param code
     * @param data
     * @param reply
     * @param option
     * @return
     * @throws RemoteException
     */
    @Override
    public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
        int result = 0;
        if(code == DATACODE){
            //如果请求码匹配
            HiLog.info(LABEL_LOG, "请求码匹配。");
                String interfaceToken = data.readInterfaceToken();
                if(DATAINTERFACETOKEN.equals(interfaceToken)){
                    //如果token匹配
                    //拿请求中的数据
                    int a = data.readInt();
                    int b = data.readInt();
                    HiLog.info(LABEL_LOG, "a = " + a);
                    HiLog.info(LABEL_LOG, "b = " + b);
                    result = a + b;
                    HiLog.info(LABEL_LOG, "result = " + result);
                    //给响应加数据

                    reply.writeInterfaceToken(REPLYINTERFACETOKEN);
                    reply.writeInt(result);
                    return true;
                }
            }
        return false;
    }
}
